<?php 
class AdCatAdmin
{
	/*
	public function ControllerAdCat()
	{
		self::runPreActions();
		self::runPostActions();
	}
	*/
	public function runPreActions()
	{
		global $commander;
		global $adc;
		switch($commander->getPostAction())
		{	
			case 'list_cat':
				if($_POST['new_item_title'] != '')
				{ self::addAdCat($_POST['new_item_title']); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'am_categories', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'am_categories', 0); }
				if($_POST['del_item_id'] > 0)
				{ self::deleteAdCat($_POST['del_item_id']); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $adc;
		
		switch ($_POST['action'])
		{
			case 'list_cat':
				if((count($_POST['Id']) > 0) && ($adc->getAdCats()))
				{
					$i = 0;
					foreach($_POST['Id'] as $id)
					{
						if(isset($adc->AdCats[$id]))
						{
							if($_POST['Title'][$i] !== $adc->AdCats[$id]->getTitle())
							{ 
								$adc->AdCats[$id]->setTitle($commander->formatText($_POST['Title'][$i]));
								$adc->AdCats[$id]->setDataChanged(true);
							}
							if($_POST['TitleEn'][$i] !== $adc->AdCats[$id]->getTitleEn())
							{ 
								$adc->AdCats[$id]->setTitleEn($commander->formatText($_POST['TitleEn'][$i]));
								$adc->AdCats[$id]->setDataChanged(true);
							}
							if($_POST['TypeId'][$i] != $adc->AdCats[$id]->getTypeId())
							{ 
								$adc->AdCats[$id]->setTypeId($_POST['TypeId'][$i]);
								$adc->AdCats[$id]->setDataChanged(true);
							}
							if($_POST['ParentId'][$i] != $adc->AdCats[$id]->getParentId())
							{ 
								$adc->AdCats[$id]->setParentId($_POST['ParentId'][$i]);
								$adc->AdCats[$id]->setDataChanged(true);
							}
							if($_POST['FormId'][$i] != $adc->AdCats[$id]->getFormId())
							{ 
								$adc->AdCats[$id]->setFormId($_POST['FormId'][$i]);
								$adc->AdCats[$id]->setDataChanged(true);
							}
							if($_POST['FormBuyId'][$i] != $adc->AdCats[$id]->getFormBuyId())
							{ 
								$adc->AdCats[$id]->setFormBuyId($_POST['FormBuyId'][$i]);
								$adc->AdCats[$id]->setDataChanged(true);
							}
							$adc->AdCats[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($adc->AdCats[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$adc->AdCats[$id]->setActive(1);
									$commander->setTableActive($id, 'am_categories', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$adc->AdCats[$id]->setActive(0);
									$commander->setTableActive($id, 'am_categories', 0); 
								}
							}
						}
					}
					if($_POST['up'] > 0)
					{ $commander->setTableItemOrderUp($_POST['up'], 'am_categories', 'CatOrder', 'typeId'); }
					if($_POST['down'] > 0)
					{ $commander->setTableItemOrderDown($_POST['down'], 'am_categories', 'CatOrder', 'typeId'); }
				}
			break;
			//List-Editor group actions end
			
			//Editor AdCat actions
			case 'editor_cat':
				if($adc->getCurrentAdCatId() > 0)
				{
					if(is_object($adc->getCurrentAdCat()))
					{
						if(stripslashes($_POST['Title']) !== $adc->CurrentAdCat->getTitle())
						{ 
							$adc->CurrentAdCat->setTitle($commander->formatText($_POST['Title'])); 
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(stripslashes($_POST['TitleEn']) !== $adc->CurrentAdCat->getTitleEn())
						{ 
							$adc->CurrentAdCat->setTitleEn($commander->formatText($_POST['TitleEn'])); 
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(intval($_POST['TypeId']) != $adc->CurrentAdCat->getTypeId())
						{ 
							$adc->CurrentAdCat->setTypeId(intval($_POST['TypeId']));
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(intval($_POST['ParentId']) != $adc->CurrentAdCat->getParentId())
						{ 
							$adc->CurrentAdCat->setParentId(intval($_POST['ParentId']));
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(intval($_POST['FormId']) != $adc->CurrentAdCat->getFormId())
						{ 
							$adc->CurrentAdCat->setFormId(intval($_POST['FormId']));
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(intval($_POST['FormBuyId']) != $adc->CurrentAdCat->getFormBuyId())
						{ 
							$adc->CurrentAdCat->setFormBuyId(intval($_POST['FormBuyId']));
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(stripslashes($_POST['Description']) !== $adc->CurrentAdCat->getDescription())
						{ 
							$adc->CurrentAdCat->setDescription($commander->formatHTML($_POST['Description']));
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if($_FILES['Image'])
						{
							$file = $_FILES['Image'];
							if($file["name"] != '')
							{
								if($file['size'] < 1000000)
								{ $adc->CurrentAdCat->uploadImage($_FILES['Image']); }
								else
								{ $commander->ErrorField['Image'] = 'image file size exceded 1mb'; }
							}
							$adc->CurrentAdCat->setDataChanged(true);
						}
						if(intval($_POST['ImageDelete']) > 0)
						{ 
							$adc->CurrentAdCat->deleteImage(); 
							$adc->CurrentAdCat->setDataChanged(true);
						}
						$adc->CurrentAdCat->Update();
					}
				}
			break;
		}
	}
	
	function addAdCat($title)
	{
		global $commander;
		$title = $commander->formatText($title);
		if($title != '')
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.'am_categories (`title`)'; 
			$sql .= 'VALUES ("'.$title.'");';
			$result = mysql_query($sql);
		}
	}
	
	function deleteAdCat($id)
	{	
		if($id > 0)
		{	
			$sql = 'UPDATE '.DB_PERFIX.'am_categories SET parentId=0 WHERE parentId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'UPDATE '.DB_PERFIX.'am_ads SET catId=0 WHERE catId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'DELETE FROM '.DB_PERFIX.'am_categories WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>